package hibernatemock;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.GeneratedValue;
import javax.persistence.Transient;

import lombok.Setter;

public class MappedFieldsLookup {

	AnnotationHandler annotationHandler = new AnnotationHandler();

	public Set<Field> lookupMappedFields(Class<?> clazz, boolean deep) {
		Set<Field> mappedFields = new HashSet<Field>();
		for (Field field : clazz.getDeclaredFields())
			if (!(Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())))
				if (!fieldOrGetterHasAnnotation(field, Transient.class) && !fieldOrGetterHasAnnotation(field, GeneratedValue.class))
					if (deep || annotationHandler.isRequired(field))
						mappedFields.add(field);
		return mappedFields;
	}

	private boolean fieldOrGetterHasAnnotation(Field field, Class<? extends Annotation> annotation) {
		if (annotationHandler.fieldOrGetterHasAnnotation(field, annotation) != null)
			return true;
		return false;
	}

	public boolean isPrimitiveDefault(Object object) {
		if (object.getClass() == Boolean.class && object.equals(new Boolean(false)))
			return true;
		if (object.getClass() == Byte.class && object.equals(new Byte((byte) 0)))
			return true;
		if (object.getClass() == Character.class && object.equals(new Character((char) 0)))
			return true;
		if (object.getClass() == Short.class && object.equals(new Short((short) 0)))
			return true;
		if (object.getClass() == Integer.class && object.equals(0))
			return true;
		if (object.getClass() == Long.class && object.equals(0L))
			return true;
		if (object.getClass() == Float.class && object.equals(0F))
			return true;
		if (object.getClass() == Double.class && object.equals(0D))
			return true;
		return false;

	}

}
